package com.oplink.webems.modules.alarm.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.oplink.webems.common.utils.DateUtils;
import com.oplink.webems.modules.alarm.service.anno.FieldMeta;
import com.oplink.webems.modules.cfg.entity.CfgNe;
import com.oplink.webems.modules.cfg.entity.CfgSlot;

/**
 * Current Alarm Entity 
 * @author yizhang
 */
@Entity
@Table(name = "alarm_cur")
@DynamicInsert 
@DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class AlarmCurrent {

	@FieldMeta(name="ID",oid="") 
	private Long id;
	
	@FieldMeta(name="机盘号",oid="1.3.6.1.4.1.2011.2.25.100.5.1.2.1.1002$1.3.6.1.4.1.2011.2.25.100.5.1.3.1.1002")
    private CfgSlot slot;
	
	@FieldMeta(name="告警ID",oid="1.3.6.1.4.1.2011.2.25.100.5.1.8.1.1001")
    private String aid;
    
	@FieldMeta(name="告警索引号",oid="1.3.6.1.4.1.2011.2.25.100.5.1.8.1.1002")
    private String aidx;
    
    //Trap类型（底层称为告警类型，分为fault,event,restore，即错误、事件、恢复三种）
	@FieldMeta(name="Trap类型",oid="1.3.6.1.4.1.2011.2.25.100.5.1.8.1.1003")
    private TrapType traptype;
    
    //告警类型（底层称为网元管理类型）
	@FieldMeta(name="告警类型",oid="1.3.6.1.4.1.2011.2.25.100.5.1.8.1.1004")
    private AlarmType atype;
    
    //机架号（来自于位置信息）
	@FieldMeta(name="机架号",oid="")
    private int chassis;
    
    //端口号（来自于位置信息）
	@FieldMeta(name="端口号",oid="1.3.6.1.4.1.2011.2.25.100.5.1.3.1.1003")
    private int port;
    
    //严重程度（底程称为告警等级）
	@FieldMeta(name="严重程度",oid="1.3.6.1.4.1.2011.2.25.100.5.1.8.1.1006")
    private AlarmSeverity severity;
    
    //告警时间
	@FieldMeta(name="告警时间",oid="1.3.6.1.4.1.2011.2.25.100.5.1.8.1.1007")
    private Date adate;
    
    //位置信息(告警位置)
	@FieldMeta(name="告警位置",oid="1.3.6.1.4.1.2011.2.25.100.5.1.8.1.1005")
    private String position;
    
    //工作模式
	@FieldMeta(name="工作模式",oid="1.3.6.1.4.1.2011.2.25.100.5.1.3.1.144")
    private AlarmOpsmMode amode;
    
    //SCC端口ID
	@FieldMeta(name="SCC端口ID",oid="1.3.6.1.4.1.2011.2.25.100.5.1.2.1.1003")
    private String sccport;
	
    //位置信息(通道位置)
	@FieldMeta(name="通道位置",oid="1.3.6.1.4.1.2011.2.25.100.5.1.3.1.146")
	private String opsmposition;
	
	//时间
	@FieldMeta(name="时间",oid="")
	private Date sdate;
	
	//网元
	@FieldMeta(name="网元",oid="")
    private CfgNe nenode;
    
	@FieldMeta(name="时间字符",oid="")
    private String atime;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="slot")
	@NotFound(action = NotFoundAction.IGNORE)	
	@JsonIgnore
	public CfgSlot getSlot() {
		return slot;
	}

	public void setSlot(CfgSlot slot) {
		this.slot = slot;
	}

	public String getAid() {
		return aid;
	}

	public void setAid(String aid) {
		this.aid = aid;
	}

	public String getAidx() {
		return aidx;
	}

	public void setAidx(String aidx) {
		this.aidx = aidx;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="traptype")
	@NotFound(action = NotFoundAction.IGNORE)	
	@JsonIgnore	
	public TrapType getTraptype() {
		return traptype;
	}

	public void setTraptype(TrapType traptype) {
		this.traptype = traptype;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="alarmtype")
	@NotFound(action = NotFoundAction.IGNORE)	
	@JsonIgnore	
	public AlarmType getAtype() {
		return atype;
	}

	public void setAtype(AlarmType atype) {
		this.atype = atype;
	}

	public int getChassis() {
		return chassis;
	}

	public void setChassis(int chassis) {
		this.chassis = chassis;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="severity")
	@NotFound(action = NotFoundAction.IGNORE)	
	@JsonIgnore		
	public AlarmSeverity getSeverity() {
		return severity;
	}

	public void setSeverity(AlarmSeverity severity) {
		this.severity = severity;
	}

	@Temporal(TemporalType.TIMESTAMP)
	//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getAdate() {
		return adate;
	}

	public void setAdate(Date adate) {
		this.adate = adate;
	}
	
	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}	
	
	@Transient
	public String getAtime() {
		this.atime = DateUtils.getDateString(this.adate, "yyyy-MM-dd HH:mm:ss");
		//System.out.println("this.adate this.adate >>> "+this.adate);
		//this.atime = "2014-02-02 12:12:05";
		return atime;
	}

	public void setAtime(String atime) {
		this.atime = atime;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="omode")
	@NotFound(action = NotFoundAction.IGNORE)	
	@JsonIgnore	
	public AlarmOpsmMode getAmode() {
		return amode;
	}

	public void setAmode(AlarmOpsmMode amode) {
		this.amode = amode;
	}

	public String getSccport() {
		return sccport;
	}

	public void setSccport(String sccport) {
		this.sccport = sccport;
	}

	public String getOpsmposition() {
		return opsmposition;
	}

	public void setOpsmposition(String opsmposition) {
		this.opsmposition = opsmposition;
	}

	public Date getSdate() {
		//sdate = DateUtils.getNowDate();
		return sdate;
	}

	public void setSdate(Date sdate) {
		this.sdate = sdate;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="neid")
	@NotFound(action = NotFoundAction.IGNORE)	
	@JsonIgnore		
	public CfgNe getNenode() {
		return nenode;
	}

	public void setNenode(CfgNe nenode) {
		this.nenode = nenode;
	}
	
	/**
	 * 用于确定告警的唯一的物理位置
	 * @return
	 */
	@Transient
	public String getCompositeKey(){
	  String ckey = "";
	  if ((this.getNenode()!=null)&&(this.getAid()!=null)){
		ckey = this.getNenode().getIp()+this.getChassis()+ (this.getSlot()==null?"":this.getSlot().getNum()) +this.getPort()+this.getAid();
	  }
	  return ckey;
	}
}
